/*
 * CopTool.java
 *
 * Created on __DATE__, __TIME__
 */

package com.gcb.swing.copy.alpha;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * 
 * @author Creat
 *
 */
public class CopyTool extends javax.swing.JFrame implements ActionListener,
		ListSelectionListener {

	public CopyTool() {
		// TODO Auto-generated constructor stub
		initComponents();
		init();
	}

	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		jList1 = new javax.swing.JList();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();
		jScrollPane2 = new javax.swing.JScrollPane();
		jList2 = new javax.swing.JList();
		jButton3 = new javax.swing.JButton();
		jTextField1 = new javax.swing.JTextField();
		jTextField2 = new javax.swing.JTextField();
		jButton4 = new javax.swing.JButton();
		jButton5 = new javax.swing.JButton();
		jTextField3 = new javax.swing.JTextField();
		jLabel1 = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jScrollPane1.setViewportView(jList1);

		jButton1.setText("\u6253\u5f00");
		jButton1.setActionCommand("open");

		jButton2.setText("\u6253\u5f00");
		jButton2.setActionCommand("save");

		jScrollPane2.setViewportView(jList2);

		jButton3.setText("\u590d\u5236");
		jButton3.setActionCommand("copy");

		jButton4.setText("\u641c\u7d22\u8d44\u6599\u5e93");
		jButton4.setActionCommand("search");

		jButton5.setText("\u521d\u59cb\u5316");
		jButton5.setActionCommand("reset");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addContainerGap(
																				62,
																				Short.MAX_VALUE)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								false)
																						.addGroup(
																								layout
																										.createSequentialGroup()
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																										.addGroup(
																												layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.LEADING)
																														.addComponent(
																																jTextField2,
																																javax.swing.GroupLayout.Alignment.TRAILING,
																																javax.swing.GroupLayout.PREFERRED_SIZE,
																																141,
																																javax.swing.GroupLayout.PREFERRED_SIZE)
																														.addGroup(
																																javax.swing.GroupLayout.Alignment.TRAILING,
																																layout
																																		.createSequentialGroup()
																																		.addComponent(
																																				jTextField3,
																																				javax.swing.GroupLayout.PREFERRED_SIZE,
																																				94,
																																				javax.swing.GroupLayout.PREFERRED_SIZE)
																																		.addGap(
																																				18,
																																				18,
																																				18)
																																		.addComponent(
																																				jButton1)
																																		.addGap(
																																				19,
																																				19,
																																				19))))
																						.addComponent(
																								jScrollPane1,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								159,
																								javax.swing.GroupLayout.PREFERRED_SIZE))
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addGroup(
																								layout
																										.createSequentialGroup()
																										.addGap(
																												47,
																												47,
																												47)
																										.addComponent(
																												jButton4)
																										.addGap(
																												48,
																												48,
																												48)
																										.addComponent(
																												jButton5))
																						.addGroup(
																								layout
																										.createSequentialGroup()
																										.addGap(
																												13,
																												13,
																												13)
																										.addComponent(
																												jButton3)
																										.addGap(
																												35,
																												35,
																												35)
																										.addGroup(
																												layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.LEADING,
																																false)
																														.addGroup(
																																javax.swing.GroupLayout.Alignment.TRAILING,
																																layout
																																		.createSequentialGroup()
																																		.addComponent(
																																				jTextField1)
																																		.addPreferredGap(
																																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																		.addComponent(
																																				jButton2))
																														.addComponent(
																																jScrollPane2,
																																javax.swing.GroupLayout.PREFERRED_SIZE,
																																180,
																																javax.swing.GroupLayout.PREFERRED_SIZE)))))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				221,
																				221,
																				221)
																		.addComponent(
																				jLabel1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				141,
																				javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addContainerGap(62, Short.MAX_VALUE)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				29,
																				29,
																				29)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								jButton2)
																						.addComponent(
																								jButton1)
																						.addComponent(
																								jTextField1,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								jTextField3,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE))
																		.addGap(
																				50,
																				50,
																				50)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jScrollPane1,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								jScrollPane2,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE))
																		.addGap(
																				39,
																				39,
																				39))
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				jButton3)
																		.addGap(
																				95,
																				95,
																				95)))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(jLabel1)
										.addGap(28, 28, 28)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jTextField2,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jButton4)
														.addComponent(jButton5))
										.addGap(80, 80, 80)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void init() {
		fileListModel1 = new FileListModel();
		fileListModel2 = new FileListModel();

		Toolkit toolkit = Toolkit.getDefaultToolkit();
		int x = (int) (toolkit.getScreenSize().getWidth() - getWidth()) / 2;
		int y = (int) (toolkit.getScreenSize().getHeight() - getHeight()) / 2;
		setLocation(x, y);

		jButton1.addActionListener(this);
		jButton2.addActionListener(this);
		jButton3.addActionListener(this);
		jButton3.setEnabled(false);
		jButton4.addActionListener(this);
		jButton5.addActionListener(this);
		jList1.setModel(fileListModel1);
		jList1.addListSelectionListener(this);
		jList2.setModel(fileListModel2);
		jList2.addListSelectionListener(this);
		jTextField1.setText("F:/saveDir");
		jTextField3.setEnabled(false);
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new CopyTool().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton3;
	private javax.swing.JButton jButton4;
	private javax.swing.JButton jButton5;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JList jList1;
	private javax.swing.JList jList2;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JTextField jTextField1;
	private javax.swing.JTextField jTextField2;
	private javax.swing.JTextField jTextField3;
	// End of variables declaration//GEN-END:variables
	private JFileChooser jFileChooser1;
	private FileListModel fileListModel1;
	private FileListModel fileListModel2;

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		boolean success = true;
		String command = e.getActionCommand();
		if ("open".equals(command)) {
			jFileChooser1 = new JFileChooser();
			jFileChooser1.setMultiSelectionEnabled(true);
			jFileChooser1.setCurrentDirectory(new File("F:\\"));
			int i = jFileChooser1.showOpenDialog(this);
			if (i == JFileChooser.APPROVE_OPTION) {
				jTextField3.setText(jFileChooser1.getCurrentDirectory()
						.getPath());
				File[] files = jFileChooser1.getSelectedFiles();
				List list = DataHandler.array2List(files);
				fileListModel1.updateData(list);
			}
		} else if ("save".equals(command)) {
			File saveDir = new File("F:/saveDir");
			if (!saveDir.exists())
				saveDir.mkdirs();
			jFileChooser1 = new JFileChooser();
			jFileChooser1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			jFileChooser1.setCurrentDirectory(saveDir);
			int i = jFileChooser1.showSaveDialog(this);
			if (i == JFileChooser.APPROVE_OPTION) {
				File file = jFileChooser1.getSelectedFile();
				jTextField1.setText(file.getPath());
			}
		} else if ("copy".equals(command)) {
			Object[] objs = jList1.getSelectedValues();
			String txt = jTextField1.getText();
			if ("".equals(txt)) {
				JOptionPane.showMessageDialog(this, "目标路径不能为空", "错误信息",
						JOptionPane.ERROR_MESSAGE);
				return;
			}
			for (int i = 0; i < objs.length; i++) {
				File file = null;
				if (objs[i] instanceof File) {
					file = (File) objs[i];
				} else if (objs[i] instanceof String) {
					file = new File((String) objs[i]);
				} else {
					return;
				}
				try {
					/*
					 * 线程不完美，把速度变慢了
					 */
					//DataHandler dh = new DataHandler(file.getPath(), txt);
					//dh.start();
					//dh.sleep(1000);
					DataHandler.copy2(file.getPath(), txt);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					success = false;
					JOptionPane.showMessageDialog(this, "复制错误", "错误信息",
							JOptionPane.ERROR_MESSAGE);
					return;
				}
			}
			if (success) {
				List list = DataHandler.array2List(objs);
				fileListModel2.updateData(list);
				String mess = "成功复制" + objs.length + "个文件";
				JOptionPane.showMessageDialog(this, mess, "信息",
						JOptionPane.INFORMATION_MESSAGE);
			}
		} else if ("search".equals(command)) {
			String txt = jTextField2.getText();
			if ("".equals(txt.trim())) {
				JOptionPane.showMessageDialog(this, "请输入搜索关键字", "错误信息",
						JOptionPane.ERROR_MESSAGE);
				return;
			}
			List list = DataHandler.fileSearch("F:\\indexDir", txt, 10);
			fileListModel1.updateData(list);
			jLabel1.setText("总共找到" + list.size() + "条符合的记录");
		} else if ("reset".equals(command)) {
			Long start_time = System.currentTimeMillis();
			DataHandler.fileIndex("F:\\indexDir", "F:\\dataDir");
			jLabel1.setText("初始化，总共花费" + 
					(System.currentTimeMillis()-start_time) + "毫秒");
		}
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		// TODO Auto-generated method stub
		jButton3.setEnabled(!jList1.isSelectionEmpty());
	}

	private class FileListModel extends DefaultListModel {

		private List data = new ArrayList();

		public void updateData(List list) {
			// TODO Auto-generated constructor stub
			data = list;
			super.fireContentsChanged(data, 0, data.size() - 1);
		}

		@Override
		public Object getElementAt(int arg0) {
			// TODO Auto-generated method stub
			return data.get(arg0);
		}

		@Override
		public int getSize() {
			// TODO Auto-generated method stub
			return data.size();
		}

		public List getData() {
			return data;
		}

		public void setData(List data) {
			this.data = data;
		}

	}

}